package com.sy.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sy.auth.service.SysMenuService;
import com.sy.auth.service.SysUserService;
import com.sy.common.config.exception.CustomException;
import com.sy.common.jwt.JwtHelper;
import com.sy.common.result.Result;
import com.sy.common.utils.MD5;
import com.sy.model.system.SysMenu;
import com.sy.model.system.SysUser;
import com.sy.vo.system.LoginVo;
import com.sy.vo.system.RouterVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "登录")
@RestController
@RequestMapping("/admin/system/index")
public class IndexController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysMenuService sysMenuService;
    //login
    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo){

        // 1. 根据用户名查询数据库
        String username = loginVo.getUsername();
        SysUser user = sysUserService.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, username));

        // 2. 判断用户是否存在
        if (user == null) {
            throw new CustomException(201, "用户不存在");
        }

        // 3. 验证密码
        String pws_db = user.getPassword();
        String psw = MD5.encrypt(loginVo.getPassword());
        if (!pws_db.equals(psw)) {
            throw new CustomException(201, "密码错误");
        }

        // 4. 用户是否被禁用
        if (user.getStatus().intValue() == 0) {
            throw new CustomException(201, "用户已经被禁用，请联系管理员");
        }

        // 5. 生成token
        String token = JwtHelper.createToken(user.getId(), user.getUsername());

        // 6. 返回token
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        return Result.ok(map);
    }

    //info
    @GetMapping("info")
    public Result info(HttpServletRequest request){

        // 1. 从请求头中获取token
        String token = request.getHeader("token");

        // 2. 从token中获得用户id或者名称，把用户信息查出来
        Long userId= JwtHelper.getUserId(token);
        SysUser sysUser = sysUserService.getById(userId);

        // 3. 根据用户Id获得可操作菜单
        List<RouterVo> routerList = sysMenuService.findUserMenuListByUserId(userId);

        // 4. 根据用户Id获得可操作按钮
        List<String> permsList = sysMenuService.findUserPermsByUserId(userId);

        // 6. 返回相关数据
        Map<String, Object> map = new HashMap<>();
        map.put("roles", "[admin]");
        map.put("name", sysUser.getName());
        map.put("avatar","https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");

        map.put("routers", routerList);
        map.put("buttons", permsList);

        return Result.ok(map);
    }

    @PostMapping ("logout")
    public Result logout(){
        return Result.ok();
    }

}
